#include "include.h"
#include "func.h"
#include "func_music.h"

#if (GUI_SELECT != GUI_NO)

#if !GUI_LCD_EN
AT(.text.func.music.display)
void func_music_display(void)
{
    gui_box_process();

    switch (box_cb.disp_sta) {
    case GUI_BOX_NULL:
        if(f_msc.pause)
            gui_display(DISP_MUSIC_PAUSE);
        else
            gui_display(DISP_MUSIC_PLAY);
        break;
    default:
        gui_box_display();
        break;
    }
}
#endif

AT(.text.func.music.display)
void func_music_enter_display(void)
{
#if GUI_LCD_EN
    f_msc.draw_update = 1;
#endif
}

AT(.text.func.music.display)
void func_music_exit_display(void)
{
}

#if GUI_LCD_EN
AT(.text.func.music.display)
void func_music_disp_stauts(void)
{
    u32 bit_rate = get_music_bitrate();
    if (bit_rate != f_msc.bit_rate) {
        f_msc.bit_rate = bit_rate;
        f_msc.sta_update = 1;
    }
    if ((f_msc.disptime.min != f_msc.curtime.min) || (f_msc.disptime.sec != f_msc.curtime.sec)) {
        f_msc.disptime.min = f_msc.curtime.min;
        f_msc.disptime.sec = f_msc.curtime.sec;
        f_msc.time_update = 1;
    }
    if (f_msc.sta_update) {
        f_msc.sta_update = 0;
        lcd_line_clear(0);
        lcd_draw_image(RES_BUF_BMP_SYS_DAT, BMP_SYS_SING, 0, 0);
        if (sys_cb.play_mode == SINGLE_MODE) {
            lcd_draw_image(RES_BUF_BMP_SYS_DAT, BMP_SYS_PM_ONE,     17, 0);
        } else if (sys_cb.play_mode == FLODER_MODE) {
            lcd_draw_image(RES_BUF_BMP_SYS_DAT, BMP_SYS_PM_FLODER,  17, 0);
        } else if (sys_cb.play_mode == RANDOM_MODE) {
            lcd_draw_image(RES_BUF_BMP_SYS_DAT, BMP_SYS_PM_RANDOM,  17, 0);
        } else {
            lcd_draw_image(RES_BUF_BMP_SYS_DAT, BMP_SYS_PM_ALL,     17, 0);
        }
        lcd_disp_num(f_msc.file_num, 18+15, 0, DN_RIGHT | DN_ZERO | DN_SMALL | 4);
        lcd_disp_num(f_msc.file_total, 18+15, 1, DN_RIGHT | DN_ZERO | DN_SMALL | 4);
        lcd_draw_image(RES_BUF_BMP_SYS_DAT, lcd_eq_table[sys_cb.eq_mode], 17+15+34, 0);
        lcd_draw_image(RES_BUF_BMP_SYS_DAT, lcd_msc_type_table[f_msc.type - 1], 86, 0);
        lcd_disp_num(f_msc.bit_rate/1000, 88, 0, DN_RIGHT | DN_ZERO | DN_SSMALL | 3);
        if (f_msc.cur_dev == DEV_UDISK) {
            lcd_draw_image(RES_BUF_BMP_SYS_DAT, BMP_SYS_UDISK, 110, 0);
        } else {
            lcd_draw_image(RES_BUF_BMP_SYS_DAT, BMP_SYS_SDCARD, 110, 0);
        }
        lcd_line_update(0, 2);
    }

    if ((f_msc.time_update) && (!bsp_lrc_is_ready())) {
        f_msc.time_update = 0;
        lcd_line_clear(2);
        if (f_msc.pause) {
            lcd_draw_image(RES_BUF_BMP_SYS_DAT, BMP_SYS_PAUSE, 0, 2);
        } else {
            lcd_draw_image(RES_BUF_BMP_SYS_DAT, BMP_SYS_PLAY, 0, 2);
        }
        lcd_disp_medium_time(f_msc.curtime.min, f_msc.curtime.sec, 21, 2);
        lcd_draw_image(RES_BUF_BMP_SYS_DAT, BMP_SYS_SPRIT, 60, 2);
        if (f_msc.alltime.min != 0xff) {
            lcd_disp_medium_time(f_msc.alltime.min, f_msc.alltime.sec, 68, 2);
        }
        lcd_line_update(2, 2);
    }
}

AT(.text.func.music.display)
void func_music_disp_fname(void)
{
    if (f_msc.fname_update) {
        f_msc.fname_update = 0;
        nmv_cb.wide = get_name_str_width(f_msc.fname);
        if (nmv_cb.wide > LCD_WIDTH) {
            nmv_cb.en = 1;
        }
        lcd_line_clear(4);
        lcd_disp_str(f_msc.fname, 0, 4, 1);
    }

    if (nmv_cb.en) {
        if (nmv_cb.ms_cnt >= 4) {
            nmv_cb.ms_cnt = 0;
            if ((nmv_cb.x + (s16)nmv_cb.wide) > 106) {
                if (nmv_cb.x == 0) {
                    nmv_cb.hold_cnt++;
                    if (nmv_cb.hold_cnt >= 2) {     //文件名头hold 800ms显示
                        nmv_cb.hold_cnt = 0;
                        nmv_cb.x--;
                    }
                } else {
                    nmv_cb.x--;
                }
            } else {
                nmv_cb.x = 0;
            }
            lcd_line_clear(4);
            lcd_disp_str(f_msc.fname, nmv_cb.x, 4, 1);
        }
    }
}

AT(.text.func.music.display)
void func_music_disp_freq_spectrum(void)
{
    if ((nmv_cb.ms_cnt % 5) == 0) {
        lcd_line_clear(6);
        lcd_disp_freq_spectrum_line(6);
        lcd_line_update(6, 2);
    }
}


AT(.text.func.music.display)
void func_music_disp_lrc(void)
{
    char *str = f_msc.fname;
    if (f_msc.lrc_update) {
        f_msc.lrc_update = 0;
        lcd_line_clear(2);
        str = lcd_draw_text(str, 0, 2, 0);
        lcd_line_clear(4);
        str = lcd_draw_text(str, 0, 4, 0);
        lcd_line_clear(6);
        str = lcd_draw_text(str, 0, 6, 0);
        lcd_line_update(2, 6);
    }
}


AT(.text.func.music.display)
void func_music_display(void)
{
    if (gui_box_process() && (!box_cb.disp_sta)) {
        //从控件显示回到主界面
        f_msc.sta_update = 1;
        f_msc.time_update = 1;
        f_msc.fname_update = 1;
        f_msc.lrc_update = 1;
    }

    switch (box_cb.disp_sta) {
    case GUI_BOX_NULL:
    case GUI_BOX_NUMBOX:
        #if CHARACTER_ENC
        if(f_msc.unicode_r & 0x02)
        {
            f_msc.unicode_r |= 0x01;
        }
        #endif // CHARACTER_ENC
        lcd_disp_music();
        break;
    default:
        gui_box_display();
        break;
    }
}
#endif

#endif



#if MUSIC_SETTING_UI_EN

typedef struct _ITEM_NODE {
    const char *disp_str;
    struct _ITEM_NODE *father;
    struct _ITEM_NODE *first_child;
    u8 child_cur;
    u8 child_total;
    u8 level;
    u8 valid_sel;
} ITEM_NODE;

enum {
    NODE_HEAD = 0,
    //一级菜单
    NODE_PLAY_MODE,
    NODE_EQ_MOD,
    NODE_FOLDER,
    NODE_DELETE_FILES,
    NODE_FOLDER_RETURN,

    //NODE_PLAY_MODE 二级菜单
    NODE_REPEAT_ALL,
    NODE_REPEAT_FOLDER,
    NODE_REPEAT_ONE,
    NODE_RANDOM,
    NODE_PLAY_MODE_RETURN,

    //NODE_EQ_MOD 二级菜单
    NODE_NORMAL,
    NODE_ROCK,
    NODE_POP,
    NODE_CLASSIC,
    NODE_JAZZ,
    NODE_COUNTRY,
    NODE_EQ_MOD_RETURN,
    //end
    NODE_MAX_NUM,
};

ITEM_NODE mcs_node[NODE_MAX_NUM];

typedef struct _MUSIC_DISP_SETTING
{
    u8 run;
    ITEM_NODE *cur_disp_node;
} MUSIC_DISP_SETTING;

MUSIC_DISP_SETTING  mcs;
const char *mcs_str_return = "Return";
const char *mcs_str_Folder = "Folder";
const char *mcs_str_Delete_Files = "Delete Files";
const char *mcs_str_Repeat_ALL = "Repeat ALL";
const char *mcs_str_Repeat_Folder = "Repeat Folder";
const char *mcs_str_Repeat_One = "Repeat One";
const char *mcs_str_Repeat_Random = "Repeat Random";
const char *mcs_str_Normal = "Normal";
const char *mcs_str_Rock = "Rock";
const char *mcs_str_Pop = "Pop";
const char *mcs_str_Classic = "Classic";
const char *mcs_str_Jazz = "Jazz";
const char *mcs_str_Country = "Country";
void music_setting_disp_item(ITEM_NODE *node);

void music_setting_disp_select_action(ITEM_NODE *node)
{
    if (NULL == node) {
        return;
    }
    const char* valid_str = (node->first_child + node->valid_sel)->disp_str;
    printf("valid_str = %s\n",valid_str);
    if (((strcmp(valid_str,mcs_str_return)) == 0) && (0 == node->level)) {  //exit music setting display
        printf("---->music setting exit\n");
        f_msc.setting_run = 0;
        f_msc.draw_update = 1;
    }else if (strcmp(valid_str,mcs_str_Folder) == 0) {
        printf("----->Action mcs_str_Folder\n");
        func_music_file_navigation();
        music_setting_disp_item(mcs.cur_disp_node);
    } else if (strcmp(valid_str,mcs_str_Delete_Files) == 0) {
        printf("----->Action mcs_str_Delete_Files\n");
    } else if (strcmp(valid_str,mcs_str_Repeat_ALL) == 0) {
        printf("----->Action mcs_str_Repeat_ALL\n");
        sys_cb.play_mode = NORMAL_MODE;
    } else if (strcmp(valid_str,mcs_str_Repeat_Folder) == 0) {
        printf("----->Action mcs_str_Repeat_Folder\n");
        sys_cb.play_mode = FLODER_MODE;
    } else if (strcmp(valid_str,mcs_str_Repeat_One) == 0) {
        printf("----->Action mcs_str_Repeat_One\n");
        sys_cb.play_mode = SINGLE_MODE;
    } else if (strcmp(valid_str,mcs_str_Repeat_Random) == 0) {
        printf("----->Action mcs_str_Repeat_Random\n");
        sys_cb.play_mode = RANDOM_MODE;
    } else if (strcmp(valid_str,mcs_str_Normal) == 0) {
        printf("----->Action mcs_str_Normal\n");
        sys_cb.eq_mode = 0;
        music_set_eq_by_num(sys_cb.eq_mode);
    } else if (strcmp(valid_str,mcs_str_Rock) == 0) {
        printf("----->Action mcs_str_Rock\n");
        sys_cb.eq_mode = 1;
        music_set_eq_by_num(sys_cb.eq_mode);
    } else if (strcmp(valid_str,mcs_str_Pop) == 0) {
        printf("----->Action mcs_str_Pop\n");
        sys_cb.eq_mode = 2;
        music_set_eq_by_num(sys_cb.eq_mode);
    } else if (strcmp(valid_str,mcs_str_Classic) == 0) {
        printf("----->Action mcs_str_Classic\n");
        sys_cb.eq_mode = 3;
        music_set_eq_by_num(sys_cb.eq_mode);
    } else if (strcmp(valid_str,mcs_str_Jazz) == 0) {
        printf("----->Action mcs_str_Jazz\n");
        sys_cb.eq_mode = 4;
        music_set_eq_by_num(sys_cb.eq_mode);
    } else if (strcmp(valid_str,mcs_str_Country) == 0) {
        printf("----->Action mcs_str_Country\n");
        sys_cb.eq_mode = 5;
        music_set_eq_by_num(sys_cb.eq_mode);
    }
}

void item_init(ITEM_NODE *node,ITEM_NODE *father, ITEM_NODE *first_child, u8 level, u16 disp_idx, const char* disp_str)
{
    node->disp_str = disp_str;
    node->father = father;
    node->first_child = first_child;
    node->child_cur = disp_idx;
    node->level = level;
    if (node->father) {
        node->father->child_total++;  //father child++
    }
}

void music_setting_disp_icon(ITEM_NODE *node, u8 line, u8 disp_idx, u8 select)
{
    //printf("node->level = %d, valid_sel = %d, child_cur = %d, line = %d, disp_idx = %d,select = %d\n",node->level, node->valid_sel , node->child_cur,line,disp_idx,select);
    if (0 == node->level) {
        if (select) {
            lcd_disp_image(RES_BUF_BMP_FUNC_DAT, BMP_FUNC_ARROW, 0, line);
        } else {
            lcd_disp_image(RES_BUF_BMP_FUNC_DAT, BMP_FUNC_STAR, 0, line);
        }
    } else {
        //printf("##cmp %s  vs %s\n",((node->first_child + disp_idx)->disp_str),mcs_str_return);
        if (strcmp( (char*)((node->first_child + disp_idx)->disp_str) ,mcs_str_return) == 0) {
            return;
        }

        if  (node->valid_sel == disp_idx) {
            lcd_disp_image(RES_BUF_BMP_FUNC_DAT, BMP_FUNC_CIRCLE_SEL, 0, line);
        } else {
            lcd_disp_image(RES_BUF_BMP_FUNC_DAT, BMP_FUNC_CIRCLE, 0, line);
        }
    }
}





AT(.text.func.music.display)
void music_setting_disp_item(ITEM_NODE *node)
{
    int i = 0, line = 0;
    u8 disp_idx;
    if (NULL == node) {
        return;
    }

#if CHARACTER_ENC
    if(f_msc.unicode_r == 0x03)
    {
        f_msc.unicode_r &= ~0x01;
    }
#endif // CHARACTER_ENC

    lcd_screen_clear();
    lcd_disp_str("Music Mode", 29, line, 1);

    if (0 == node->level) {
        node->valid_sel = node->child_cur;
    }

    for (i = 0; (i < 3); i++) {
        if (node->child_cur < 3) {
            line += 2;
            disp_idx = i;
            lcd_disp_str((char*)((node->first_child + disp_idx)->disp_str), 20, line, 1);
            if (i == node->child_cur) {
                lcd_disp_invert(20, line, 100, 2);
                lcd_line_update(line, 2);
                music_setting_disp_icon(node,line,i,1);
            } else {
                music_setting_disp_icon(node,line,i,0);
            }

        } else {
            line += 2;
            disp_idx =  i + node->child_cur - 2;
            lcd_disp_str((char*)((node->first_child + disp_idx )->disp_str), 20, line, 1);
            if (i == 2) {
                lcd_disp_invert(20, line, 100, 2);
                lcd_line_update(line, 2);
                music_setting_disp_icon(node,line,disp_idx,1);
            } else {
                music_setting_disp_icon(node,line,disp_idx,0);
            }
        }
        //printf("-->%d : %s\n",i,(char*)((node->first_child + disp_idx)->disp_str));
    }
}


AT(.text.func.music.display)
bool music_setting_disp_msg_process(u16 msg)
{
    bool ret = false;
    if (!f_msc.setting_run){
        return ret;
    }
    switch (msg) {
        case KU_PLAY:
        case KU_PLAY_POWER:
        case KU_PLAY_MODE:
           //printf("MCS KU_PLAY, child_total = %d\n",(mcs.cur_disp_node->first_child  +  mcs.cur_disp_node->child_cur)->child_total );
           //printf("child_cur = %d, child_total = %d\n",mcs.cur_disp_node->child_cur,mcs.cur_disp_node->child_total);
           //printf("mcs.cur_disp_node->disp_str = %s\n",(mcs.cur_disp_node->first_child + mcs.cur_disp_node-> child_cur)->disp_str);

           if (strcmp((mcs.cur_disp_node->first_child + mcs.cur_disp_node-> child_cur)->disp_str ,mcs_str_return) == 0) {  //返回上一级
                 if (mcs.cur_disp_node->father->child_total) {
                     mcs.cur_disp_node = mcs.cur_disp_node->father;
                     printf("disp_father\n");
                     music_setting_disp_item(mcs.cur_disp_node);
                 }
            } else if (mcs.cur_disp_node->level == 1 ) {  //选中采单做相应的动作，如EQ设置等
                printf("select Go\n");
                mcs.cur_disp_node->valid_sel = mcs.cur_disp_node->child_cur;
                music_setting_disp_item(mcs.cur_disp_node);
            } else if ((mcs.cur_disp_node->first_child  +  mcs.cur_disp_node->child_cur)->child_total) {  //到下一级菜单
                mcs.cur_disp_node = mcs.cur_disp_node->first_child + mcs.cur_disp_node->child_cur;
                music_setting_disp_item(mcs.cur_disp_node);
            }
            music_setting_disp_select_action(mcs.cur_disp_node);
            ret = true;
            break;

        case KU_PREV:
            printf("MCS KU_PREV\n");
            if (mcs.cur_disp_node->child_total) {
                if (mcs.cur_disp_node->child_cur > 0) {
                    mcs.cur_disp_node->child_cur--;
                } else {
                    mcs.cur_disp_node->child_cur = mcs.cur_disp_node->child_total - 1;
                }
                printf("child_cur = %d, child_total = %d\n",mcs.cur_disp_node->child_cur,mcs.cur_disp_node->child_total);
                music_setting_disp_item(mcs.cur_disp_node);
            }
            ret = true;
            break;

        case KU_NEXT:
               printf("MCS KU_NEXT\n");
            if (mcs.cur_disp_node->child_total) {
                if (mcs.cur_disp_node->child_cur < (mcs.cur_disp_node->child_total -1)) {
                    mcs.cur_disp_node->child_cur++;
                } else {
                    mcs.cur_disp_node->child_cur = 0;
                }
                printf("child_cur = %d, child_total = %d\n",mcs.cur_disp_node->child_cur,mcs.cur_disp_node->child_total);
                music_setting_disp_item(mcs.cur_disp_node);
            }
            ret = true;
            break;

        default:
            ret = false;
            break;
    }
    return ret;
}


AT(.text.lcd.setting*)
void music_disp_setting_init(void)
{
    printf("mcs_node addr= 0x%X, sizeof mcs_node = %d\n", mcs_node,sizeof(mcs_node));
    memset(mcs_node,0x00, sizeof(mcs_node));

    //head
    item_init(&mcs_node[NODE_HEAD], NULL, &mcs_node[NODE_PLAY_MODE],0, 0, NULL);

    //一级setting
    item_init(&mcs_node[NODE_PLAY_MODE], &mcs_node[NODE_HEAD], &mcs_node[NODE_REPEAT_ALL],1, 1,"Play Mode");
    mcs_node[NODE_PLAY_MODE].valid_sel = sys_cb.play_mode ;
    item_init(&mcs_node[NODE_EQ_MOD],  &mcs_node[NODE_HEAD], &mcs_node[NODE_NORMAL],1, 0,"Eq Mode");
    mcs_node[NODE_EQ_MOD].valid_sel = sys_cb.eq_mode;

    item_init(&mcs_node[NODE_FOLDER], &mcs_node[NODE_HEAD], &mcs_node[NODE_DELETE_FILES],1,0,mcs_str_Folder);
    item_init(&mcs_node[NODE_DELETE_FILES], &mcs_node[NODE_HEAD], NULL,1, 0, mcs_str_Delete_Files);
    item_init(&mcs_node[NODE_FOLDER_RETURN], &mcs_node[NODE_HEAD], NULL,1, 0, mcs_str_return);
    //NODE_PLAY_MODE下的二级setting
    item_init(&mcs_node[NODE_REPEAT_ALL], &mcs_node[NODE_PLAY_MODE], NULL,2, 0, mcs_str_Repeat_ALL);
    item_init(&mcs_node[NODE_REPEAT_FOLDER], &mcs_node[NODE_PLAY_MODE], NULL,2, 0,mcs_str_Repeat_Folder);
    item_init(&mcs_node[NODE_REPEAT_ONE], &mcs_node[NODE_PLAY_MODE], NULL, 2,0, mcs_str_Repeat_One);
    item_init(&mcs_node[NODE_RANDOM], &mcs_node[NODE_PLAY_MODE], NULL, 2,0,mcs_str_Repeat_Random);
    item_init(&mcs_node[NODE_PLAY_MODE_RETURN], &mcs_node[NODE_PLAY_MODE], NULL,2, 0,mcs_str_return);
    //EQ_MODE下的二级setting
    item_init(&mcs_node[NODE_NORMAL], &mcs_node[NODE_EQ_MOD], NULL, 2,0, mcs_str_Normal);
    item_init(&mcs_node[NODE_ROCK], &mcs_node[NODE_EQ_MOD], NULL, 2,0, mcs_str_Rock);
    item_init(&mcs_node[NODE_POP], &mcs_node[NODE_EQ_MOD], NULL, 2,0, mcs_str_Pop);
    item_init(&mcs_node[NODE_CLASSIC], &mcs_node[NODE_EQ_MOD], NULL,2, 0, mcs_str_Classic);
    item_init(&mcs_node[NODE_JAZZ], &mcs_node[NODE_EQ_MOD], NULL,2, 0, mcs_str_Jazz);
    item_init(&mcs_node[NODE_COUNTRY], &mcs_node[NODE_EQ_MOD], NULL,2, 0, mcs_str_Country);
    item_init(&mcs_node[NODE_EQ_MOD_RETURN], &mcs_node[NODE_EQ_MOD], NULL,2, 0, mcs_str_return);
    //Folder下的二级setting
    //printf("mcs_node[NODE_HEAD].child_total = %d\n",mcs_node[NODE_HEAD].child_total);
    //printf("mcs_node[NODE_PLAY_MODE].child_total = %d\n",mcs_node[NODE_PLAY_MODE].child_total);
    //printf("mcs_node[NODE_PLAY_MODE].disp_str = %s\n",mcs_node[NODE_PLAY_MODE].disp_str);
}


AT(.text.func.music.display)
void func_music_disp_setting(void)
{
    music_disp_setting_init();
    f_msc.setting_run = true;
    lcd_screen_clear();
    memset(&mcs,0x00, sizeof(mcs));
    mcs.cur_disp_node = &mcs_node[NODE_HEAD];
    music_setting_disp_item(mcs.cur_disp_node);
}

void func_music_disp_setting(void);


AT(.text.display.lcd)
bool lcd_disp_setting(void)
{
    if (f_msc.setting_run) {
        return true;
    }
    return false;
//    f_msc.draw_update = 1;
//    if (f_msc.draw_update) {
//        lcd_screen_clear();
//        f_msc.setting_update = 1;
//    }
}

#endif
